package doc2db.interfaces.action.save;

import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.types.TAPAbstractType;
import org.apache.poi.hwpf.usermodel.BorderCode;
import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.ParagraphProperties;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableProperties;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 *
 * @author yura
 */
public class SaveDoc implements Saver {
    /** Edit panel whith date */
    private MyJEditorPane medit ;

    /** Text of error if it hapen */
    private String error = "ancnown";

    /**
     * this.medit = medit;
     * @param medit edit panel whith date
     */
    public SaveDoc(MyJEditorPane medit) {
        this.medit = medit;
    }

    @Override
    public String err(){
        return error;
    }

    /**
     *  Edit panel whith date
     * @return Edit panel whith date
     */
    public MyJEditorPane getMedit() {
        return medit;
    }

    /**
     *  Edit panel whith date
     * @param medit Edit panel whith date
     */
    public void setMedit(MyJEditorPane medit) {
        this.medit = medit;
    }


    @Override
    public boolean save (String fname){
        return saveDoc(fname);
    }

    private boolean saveDoc (String fname){
        String mfname;
        if (new File(fname).exists()){
            mfname = "" + fname;
        } else if (fname.endsWith(".doc") | fname.endsWith(".DOC")) {
            mfname = "" + fname;
        } else {
            mfname = fname + ".doc";
        }


        final HWPFDocument doc ;
        try {
            doc = new HWPFDocument(new POIFSFileSystem(
                    SaveAsAction.class.getResourceAsStream("example.doc")));
        } catch (IOException ex) {
            error = "" + ex;
            return false;
        }

        int row;
        try {
            row = medit.table.length;
        } catch (Exception e) {
            error = "" + e;
            return false;
        }
        int col;
        try {
            col = medit.table[0].length;
            if (medit.head != null) {
                row++;
            }
        } catch (Exception e) {
            error = "" + e;
            return false;
        }


        Range overallRange = doc.getRange();
        TableProperties tp = new TableProperties(col);
        System.out.println("before - " + overallRange.numParagraphs());
        
        Table table = overallRange.insertBefore(tp, row);
        System.out.println("after - " + overallRange.numParagraphs());
        System.out.println(col * row + row);

        int ri = 0;
        if (medit.head != null){
            TableRow r = table.getRow(ri);
            r.setRowHeight(20);
            for (int j = 0; j < r.numCells(); j++) {
                /*ParagraphProperties pp = new ParagraphProperties();
                TableCell c = r.getCell(j);
                final Paragraph p = c.insertBefore(pp, 0);
                p.insertBefore(medit.head[j]);
                System.out.println(p.text() + " | " + c.text());*/
                r.getCell(j).replaceText("", medit.head[j]);//insertAfter(medit.head[j]);
            }
            System.out.println();
            ri ++;
        }

        //if (false)
        for (int i = 0; i < table.numRows() - 1; i++) {
            TableRow r = table.getRow(i + ri);
            for (int j = 0; j < r.numCells(); j++) {
                /*TableCell c = r.getCell(j);
                final Paragraph p = c.insertBefore(new ParagraphProperties(), 0);
                p.insertBefore(medit.table[i ][j ]);
                System.out.println(p.text() + " | " + c.text());*/
                r.getCell(j).replaceText("", medit.table[i][j]);//.insertAfter(medit.table[i][j]);
            }
            System.out.println();
        }

        /*Записываем в файл*/
        try {
            FileOutputStream out = new FileOutputStream(mfname);
            doc.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            error = "" + e;
            return false;
        }
        return true;
    }

}
